<?php

function godump($msg, $data=[]) {
    file_put_contents(
        '/tmp/lyf_dump.log',
        date('Y-m-d H:i:s') . "========================================================\n" . '【'.$msg.'】'.(!empty($data) ? "\n".json_encode($data) : '')."\n\n",
        FILE_APPEND
    );
}
function curl_call($uri, $params=[], $full_url=false) {
    ksort($params);
    $parr = [];
    $pstr = '';
    if(!empty($params)) {
        foreach ($params as $k => $v) {
            $parr[] = $k . '=' . $v;
        }
        $pstr = join('&', $parr);
    }

    if($full_url===true) {
        $url = $uri;
    } else {
        // $url = config('game.cfg.origin_api') . $uri . '?' . $pstr;
        $url = env('ORIGIN_API') . $uri . '?' . $pstr;
        $sig_time = \App\Http\Controllers\Controller::now();
        $sig = base64_encode(hash_hmac(
            "sha1",
            "GET\n" .
            $uri . "\n" .
            $pstr . "\n" .
            // "x-co-client:" . config('game.cfg.appid') . "\n" .
            "x-co-client:" . env('APPID') . "\n" .
            "x-co-timestamp:" . $sig_time,
            // config('game.cfg.appsecret'),
            env('APPSECRET'),
            true
        ));
    }

    $headers = [
        'Content-Type:application/json',
    ];
    if(!$full_url) {
        // $headers[] = 'X-Co-Client:' . config('game.cfg.appid');
        $headers[] = 'X-Co-Client:' . env('APPID');
        $headers[] = 'X-Co-TimeStamp:' . $sig_time;
        $headers[] = 'X-Co-Sign:' . $sig;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $output = curl_exec($ch);
    curl_close($ch);
    $follow_result = json_decode($output, true);
    if(!isset($follow_result['code'])) return false;
    else return $follow_result;
}
